package com.nuatar.meibi.pubvar;
import com.nuatar.meibi.R;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TitleGridView extends LinearLayout {
        LinearLayout titleBar;
        ImageView headImg;
        RelativeLayout tacs;
        TextView titles;
        TextView counts;
        TagsGridView gvs;
      public  BaseAdapter Adapter;
        

        public BaseAdapter getAdapter() {
			return Adapter;
		}
        public void setNumColumns(int i){
        	gvs.setNumColumns(i);
        }
		public void setAdapter(BaseAdapter adapter) {
			Adapter = adapter;
			gvs.setAdapter(Adapter);
		}
		public void recycle(){
			this.removeAllViews();
		}
		public TitleGridView(Context context) {
                super(context);
            
                init_view(context);

        }

        public TitleGridView(Context context, AttributeSet attrs) {

              
                super(context,attrs);
                init_view(context);

        }
        public TitleGridView(Context context,SortList tgi){
                super(context);
                init_view(context);
                headImg.setImageResource(R.drawable.arrow_d);
                titles.setText(tgi.title);
                counts.setText(tgi.getWallPapers().size());
                
        }

        public void init_view(Context context) {
                titleBar = new LinearLayout(context);
                headImg = new ImageView(context);
                headImg.setImageResource(R.drawable.arrow_d);
                LinearLayout.LayoutParams imgparam = new LinearLayout.LayoutParams(
                                android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                imgparam.height=60;
                imgparam.width=60;
                headImg.setLayoutParams(imgparam);
                tacs = new RelativeLayout(context);
                titles = new TextView(context);
                titles.setText("tt");
                counts = new TextView(context);
                gvs = new TagsGridView(context);
                counts.setText("2491");
                headImg.setPadding(0, 0, 0, 0);
                titles.setPadding(0, 0, 0, 0);
                counts.setPadding(0, 0, 0, 0);
                titles.setTextSize(15);
                titles.setTextColor(Color.BLACK);
                LinearLayout.LayoutParams tbparam = new LinearLayout.LayoutParams(
                                android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                tbparam.setMargins(0, 5, 0, 0);
                tbparam.height=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_d).getHeight();
                titleBar.setPadding(0, 0, 0, 0);
                titleBar.setGravity(Gravity.CENTER_VERTICAL);
                titleBar.setOrientation(LinearLayout.HORIZONTAL);
                titleBar.addView(headImg);
                titleBar.setLayoutParams(tbparam);
                RelativeLayout.LayoutParams tacparam = new RelativeLayout.LayoutParams(
                                android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                tacparam.addRule(RelativeLayout.CENTER_VERTICAL);
                tacs.setLayoutParams(tacparam);
                RelativeLayout.LayoutParams titparam = new RelativeLayout.LayoutParams(
                                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                titparam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                titparam.addRule(RelativeLayout.CENTER_VERTICAL);
                RelativeLayout.LayoutParams cparam = new RelativeLayout.LayoutParams(
                                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                cparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                cparam.addRule(RelativeLayout.CENTER_VERTICAL);
                titles.setLayoutParams(titparam);
                counts.setLayoutParams(cparam);
                tacs.addView(titles);
                tacs.addView(counts);
                titleBar.addView(tacs);
                titleBar.setBackgroundColor(Color.WHITE);           
                gvs.setNumColumns(2);
                gvs.setVerticalScrollBarEnabled(false);
                gvs.setGravity(Gravity.CENTER);
                gvs.setClickable(true);
                gvs.setFocusable(true);
                gvs.setVisibility(View.GONE);
                gvs.setBackgroundColor(Color.GRAY);
                gvs.setPadding(0, 0, 0, 0);
                addView(titleBar);
                addView(gvs);
                setOrientation(LinearLayout.VERTICAL);
                titleBar.setClickable(true);
                titleBar.setFocusable(true);
                titleBar.setOnClickListener(new OnClickListener(){
                
                        @Override
                        public void onClick(View v) {
                                
                                if(gvs.getVisibility()==View.VISIBLE){
                                        gvs.setVisibility(View.GONE);
                                        headImg.setImageResource(R.drawable.arrow_d);
                                        titleBar.setBackgroundColor(Color.WHITE);
                                }else
                                {
                                        gvs.setVisibility(View.VISIBLE);
                                        headImg.setImageResource(R.drawable.arrow_u);
                                        titleBar.setBackgroundColor(Color.LTGRAY);
                                }
                        }});
                
        }

        public void setTitle(String title) {
                titles.setText(title);
        }

        public void setTitleSize(float size) {
                titles.setTextSize(size);
        }
        public void setHeadimgParam(LayoutParams param)
        {
                headImg.setLayoutParams(param);
        } 
        
        public void setCounts(int count){
                counts.setText(String.valueOf(count));
        }
        
        public void setGridAdapter(ListAdapter gridAdapter){
                gvs.setAdapter(gridAdapter);
        }
        

}